package com.atguigu.ucenter.controller;


import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.R;
import com.atguigu.commonutils.vo.CommMemberVo;
import com.atguigu.commonutils.vo.UcenterMemberOrder;
import com.atguigu.ucenter.entity.Member;
import com.atguigu.ucenter.entity.vo.RegisterVo;
import com.atguigu.ucenter.service.MemberService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2023-04-13
 */
@CrossOrigin
@RestController
@RequestMapping("/ucenter/member")
public class MemberController {

    @Autowired
    private MemberService memberService;

    /**
     * 登录
     * 使用手机号和密码
     * @param member
     * @return
     */
    @PostMapping("/login")

    public R login(@RequestBody Member member){

        String token=memberService.login(member);

         return R.ok().data("token",token);
    }


    /**
     * 用户注册，昵称，手机号，密码，验证码
     * @param registerVo
     * @return
     */
    @PostMapping("/register")
    public R register(@RequestBody RegisterVo registerVo)
    {
       memberService.register(registerVo);
        return  R.ok();
    }

    /**
     * 根据token获取用户信息
     * @param request
     * @return
     */
    @GetMapping("/getInfo")
    public R getMemberInfo(HttpServletRequest request){

        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        Member member = memberService.getById(memberId);

        return R.ok().data("userInfo",member);
    }

    /**
     * 远程调用
     * 根据用户id返回用户信息
     * @param memberId
     * @return
     */
    @PostMapping("/getCommMemberInfo")
    public CommMemberVo getCommMemberInfo(@RequestParam("memberId")String memberId)
    {
        Member member = memberService.getById(memberId);
        CommMemberVo commMemberVo = new CommMemberVo();
        BeanUtils.copyProperties(member,commMemberVo);
        return commMemberVo;
    }

    //根据id查询用户信息
    @PostMapping("/getUcenterInfoOrder")
    public UcenterMemberOrder getUcenterInfoOrder(@RequestParam("memberId") String memberId){
        Member member = memberService.getById(memberId);
        UcenterMemberOrder ucenterMemberOrder = new UcenterMemberOrder();
        BeanUtils.copyProperties(member,ucenterMemberOrder);
        return ucenterMemberOrder;

    }

    //获取每天用户注册数量
    @GetMapping("/getMemberCount/{day}")
    public int getMemberCount(@PathVariable("day") String day){
        int count=memberService.getMemberCount(day);
        return count;
    }



}

